package com.example.twodimensioncodedemo;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.adapter.ChaXunAdapter;
import com.adapter.QueryAdapter;
import com.bean.ChaXunBean;
import com.bean.QueryBean;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.gson.Gson;
import com.utils.UrlUtlis;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import demo.zdc.com.myapplication.R;
import okhttp3.Call;

/**
 * 查询售票情况
 */
public class QueryTicket extends Activity {

    /*  @Bind(R.id.textname)
      TextView textname;*/
    @Bind(R.id.sp_selectedzhanguan)
    Spinner spSelectedzhanguan;
    @Bind(R.id.person)
    TextView person;
    @Bind(R.id.query)
    ImageView query;
    @Bind(R.id.listview)
    RecyclerView listview;
    List<QueryBean.ExhibitionListBean> exhibitionList;
    @Bind(R.id.rtn)
    ImageView rtn;
    private int index;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_query_ticket);
        ButterKnife.bind(this);
        exhibitionList = (List<QueryBean.ExhibitionListBean>) getIntent().getSerializableExtra("exhibitionLists");
        QueryAdapter queryAdapter = new QueryAdapter(QueryTicket.this, exhibitionList);
        spSelectedzhanguan.setAdapter(queryAdapter);
//
        spSelectedzhanguan.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //  view.setVisibility(View.INVISIBLE);
                //   textname.setText(exhibitionList.get(position).getExhibition());
              /*  TextView tv = (TextView) view;
                tv.setTextColor(getResources().getColor(R.color.white));    //设置颜色
                tv.setTextSize(12.0f);    //设置大小
                tv.setGravity(Gravity.VERTICAL_GRAVITY_MASK);   //设置居中*/
                index = exhibitionList.get(position).getNo();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


        //选择时间
        person.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DatePickerDialog(QueryTicket.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        person.setText(String.format("%d-%d-%d", year, monthOfYear + 1, dayOfMonth));
                    }
                }, 2000, 1, 2).show();

            }
        });

        //开始查询啦！！！
        query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // String name = textname.getText().toString().trim();
                String date = person.getText().toString().trim();
                if (/*!TextUtils.isEmpty(name) &&*/ !TextUtils.isEmpty(date) && !TextUtils.isEmpty(index + "") && !date.equals("Settlement date")) {
                    OkHttpUtils.get()
                            .url(UrlUtlis.CHAXUN)
                            .addParams("exhibition_id", index + "")
                            .addParams("clearing_date", date)
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Call call, Exception e, int id) {

                                }

                                @Override
                                public void onResponse(String response, int id) {
                                    Gson gson = new Gson();
                                    ChaXunBean chaXunBean = gson.fromJson(response, ChaXunBean.class);
                                    if (chaXunBean.getCode() == 200) {
                                        List<ChaXunBean.Search_result> lists = chaXunBean.getSearch_result();
                                        ChaXunAdapter adapter = new ChaXunAdapter(QueryTicket.this, lists);
                                        listview.setAdapter(adapter);
                                        listview.setLayoutManager(new LinearLayoutManager(QueryTicket.this, LinearLayoutManager.VERTICAL, false));

                                    }
                                }
                            });


                } else {
                    Toast.makeText(getApplication(), "不能为空", Toast.LENGTH_SHORT).show();
                }

            }
        });


        //返回
        rtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    public Action getIndexApiAction() {
        Thing object = new Thing.Builder()
                .setName("QueryTicket Page") // TODO: Define a title for the content shown.
                // TODO: Make sure this auto-generated URL is correct.
                .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
                .build();
        return new Action.Builder(Action.TYPE_VIEW)
                .setObject(object)
                .setActionStatus(Action.STATUS_TYPE_COMPLETED)
                .build();
    }

    @Override
    public void onStart() {
        super.onStart();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client.connect();
        AppIndex.AppIndexApi.start(client, getIndexApiAction());
    }

    @Override
    public void onStop() {
        super.onStop();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        AppIndex.AppIndexApi.end(client, getIndexApiAction());
        client.disconnect();
    }
}
